import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:atten_sys/entity/Entity.dart';
import 'package:atten_sys/fregments/list4Daily.dart';
import 'package:atten_sys/fregments/list4Excuse.dart';
import 'package:atten_sys/utils/http.dart';

class WorkStatus extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {

    // TODO: implement createState
    return new _WorkStatusState();
  }
}

class _WorkStatusState extends State<WorkStatus> {
  List<Daily> dailyList = new List();

  List<Excuse> excuseList = new List();

  HttpUtil _http = HttpUtil.getInstance();

  //设置两个方法来请求信息：
  //1.个人的缺勤次数
  checkDailyInfo() async {
    DailyList dl = new DailyList(dailies: new List());

    SharedPreferences preferences = await SharedPreferences.getInstance();
    Options options = Options(
        headers: {'Authorization': preferences.getString('Authorization')},
        responseType: ResponseType.json);
    dynamic data = await _http.get('/daily/app/check', options: options);
    if (data['success'] == true) {
      dl = DailyList.fromJson(data['payload']);
    }
    setState(() {
      dailyList = dl.dailies;
    });
  }

  //2.个人的请假次数
  checkExcuseInfo() async {
    ExcuseList el = new ExcuseList(excuses: new List());

    SharedPreferences preferences = await SharedPreferences.getInstance();
    Options options = Options(
        headers: {'Authorization': preferences.getString('Authorization')},
        responseType: ResponseType.json);
    int uid = await preferences.get('UID');
    dynamic data = await _http.get('/excuse/app/check/$uid', options: options);
    if (data['success'] == true) {
      print(data);
      el = ExcuseList.fromJson(data['payload']);
    }
    setState(() {
      excuseList = el.excuses;
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    checkDailyInfo();
    checkExcuseInfo();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    print('the length is ${dailyList.length}');
    return ListView(
      padding: EdgeInsets.symmetric(horizontal: 20),
            children: <Widget>[
              SizedBox(
                height: 150,
              ),
              SizedBox(
                height: 55,
                child: RaisedButton(
                  onPressed: () => Navigator.push(context,
                      MaterialPageRoute(builder: (BuildContext context) {
                        return new List4Daily(
                          list: dailyList,
                          title: '缺勤记录',
                        );
                      })),
                  colorBrightness: Brightness.dark,
                  highlightColor: Colors.blue[700],
                  splashColor: Colors.grey,
                  child: Text('缺勤次数：${dailyList.isEmpty ? 0 : dailyList.length}'),
                  color: Colors.blue,
                ),
              ),
              SizedBox(
                height: 50,
              ),
              SizedBox(
                height: 55,
                child: RaisedButton(
                  onPressed: () => Navigator.push(context,
                      MaterialPageRoute(builder: (BuildContext context) {
                        return new List4Excuse(
                          list: excuseList,
                          title: '请假记录',
                        );
                      })),
                  highlightColor: Colors.blue[700],
                  splashColor: Colors.grey,
                  colorBrightness: Brightness.dark,
                  child: Text('请假申请：${excuseList.isEmpty ? 0 : excuseList.length}'),
                  color: Colors.blue,
                ),
              ),
            ],
    );
  }
}
